package com.neuedu.shixun.hospital2.service.Impl;

import com.neuedu.shixun.hospital2.entity.Admin;
import com.neuedu.shixun.hospital2.entity.Admin2;
import com.neuedu.shixun.hospital2.entity.Depart;
import com.neuedu.shixun.hospital2.entity.RuleForm2;
import com.neuedu.shixun.hospital2.repository.AdminRepository;
import com.neuedu.shixun.hospital2.repository.DepartRepository;
import com.neuedu.shixun.hospital2.service.AdminService;
import com.neuedu.shixun.hospital2.util.MD5Util;
import com.neuedu.shixun.hospital2.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;

@Slf4j
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private DepartRepository departRepository;

    @Override
    public String save(RuleForm2 ruleForm2) {
        Admin admin=new Admin();
        admin.setAAname(ruleForm2.getName());
        try {
            admin.setAPassword(MD5Util.EncoderByMd5(ruleForm2.getPass()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        admin.setAJobnum(ruleForm2.getIdNum());
        admin.setAPhonenum(ruleForm2.getTel());
        admin.setAStatus(ruleForm2.getAstatus());
        log.info("注册的对象为："+adminRepository.save(admin));
        if (adminRepository.save(admin) != null)
            return "ok";
        else
            return "fail";
    }

    @Override
    public String getPass() {
        Random random = new Random();
        int i = random.nextInt(10000);
        String ii = i + "";
        if (ii.length() != 4) {
            for (int j = 0; j < 4 - ii.length(); j++) {
                ii += '0';
            }
        }
        redisUtil.set("pass", "" + ii);
        redisUtil.expire("pass", 60);
        long time = redisUtil.getExpire("pass");
        System.out.println(time);
        return "" + ii;
    }

    @Override
    public String comPass(String inputcode) {
        String pass = (String) redisUtil.get("pass");
//        System.out.println("验证码："+pass);
//        System.out.println("验证码："+inputcode);
        log.info("验证码：" + pass);
        log.info("输入的验证码：" + inputcode);
        if (pass.equals(inputcode)) {
//           System.out.println("ok");
            log.info("ok");
            return "ok";
        } else
            return "false";
    }
    @Override
    public String checkOne(String idNum,String aPassword,String aAname) {
        log.info(idNum+","+aPassword+","+aAname);
        Admin admin=adminRepository.findByAAnameAndAPasswordAndAJobnum(aAname,aPassword,idNum);
//                log.info("admin:"+admin);
            if (admin!=null){
                if (admin.getAStatus())
                    return "ok";
                else
                    return "no";
            }
            else
              return "false";
    }
    @Override
    public String comCode(String checkCode, String yzm) {
        if (checkCode.equals(yzm))
            return "ok";
        else
          return "false";
    }
    @Autowired
    public List<Admin> getAdmin(){
      List<Admin> list=adminRepository.findAll();
      return list;
    }

    @Override
    public List<Admin2> getAdmin2(List<Admin> list) {
        List<Admin2> list1=new ArrayList<>();
        for (Admin admin:list){
            Admin2 admin2=new Admin2();
            admin2.setAAid(admin.getAAid());
            admin2.setAAname(admin.getAAname());
            try{
            Optional<Depart> optional=departRepository.findById(admin.getADepartid());
                Depart depart=new Depart();
                if (optional.isPresent()){
                    depart=optional.get();
                }
                admin2.setADepart(depart.getDDname());
            }catch (Exception e) {
            }
            admin2.setAJobnum(admin.getAJobnum());
            admin2.setAPhonenum(admin.getAPhonenum());
            admin2.setAStatus(admin.getAStatus());
            admin2.setAPassword(admin.getAPassword());
            list1.add(admin2);
        }
        return list1;
    }

    @Override
    public Admin findOneAdmin(String jobnum) {
        return adminRepository.findByAJobnum(jobnum);
    }

    @Override
    public Admin2 getOneAdmin2(Admin admin) {
        Admin2 admin2=new Admin2();
        admin2.setAAid(admin.getAAid());
        admin2.setAAname(admin.getAAname());
        try{
        Optional<Depart> optional=departRepository.findById(admin.getADepartid());
        Depart depart=new Depart();
        if (optional.isPresent()){
            depart=optional.get();
        }
        admin2.setADepart(depart.getDDname());
        }catch (Exception e){

        }
        admin2.setAJobnum(admin.getAJobnum());
        admin2.setAPhonenum(admin.getAPhonenum());
        admin2.setAStatus(admin.getAStatus());
        admin2.setAPassword(admin.getAPassword());
        return admin2;
    }

    @Override
    public Admin updateAdmin(Admin admin) {
        log.info("要修改的admin:"+admin);
        return adminRepository.save(admin);
    }

    @Override
    public String JiaMi(String pass) {
        String pass1= null;
        try {
            pass1 = MD5Util.EncoderByMd5(pass);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return pass1;
    }


//    @Override
//    public Admin checkOne(Admin admin) {
//        adminRepository.
//        return null;
//    }
}
